package com.xxx.flink.transform;

import com.xxx.flink.customsource.CustomSource;
import com.xxx.flink.pojo.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 转换算子Mapper
 * @author guwq
 * @since 2023/7/26
 */
public class TransformMapperTest {

    public static void main(String[] args) throws Exception {
        // 流式执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 读取数据
        DataStreamSource<Event> sourceStream = env.addSource(new CustomSource());

        // Mapper方式1：静态内部类
//        sourceStream.map(new MyMapper()).print();

        // Mapper方式2：匿名内部类
//        sourceStream.map(new MapFunction<Event, String>() {
//            @Override
//            public String map(Event event) throws Exception {
//                return event.getName();
//            }
//        }).print();

        // Mapper方式3：Lambda表达式（匿名内部类里只有一个方法）
        sourceStream.map(Event::getName).print();

        // 提交执行
        env.execute();
    }

    public static class MyMapper implements MapFunction<Event, String> {

        @Override
        public String map(Event event) throws Exception {
            return event.getName();
        }
    }

}
